﻿#include "formtcpfileclient.h"
#include "ui_formtcpfileclient.h"

#include <QDateTime>
#include <QFileDialog>

FormTcpFileClient::FormTcpFileClient(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormTcpFileClient)
{
    ui->setupUi(this);

    connectAllSlots();
     this->setWindowIcon(QIcon(":/src/icon/icon_64/train.svg"));
    connect(&_tcpClient,&TcpFileClient::TcpMsg,this,&FormTcpFileClient::OnLogMsg);
    connect(&_timer,&QTimer::timeout,this,&FormTcpFileClient::HandleTimer);
    _timer.start(500);
}

FormTcpFileClient::~FormTcpFileClient()
{
    delete ui;
}


void FormTcpFileClient::on_pushButton_connect_clicked()
{
    _tcpClient.connectToServer(_strIP,_Port);
    if(QAbstractSocket::ConnectedState==_tcpClient.state())
    {
        OnLogMsg(QString::fromLocal8Bit("连接成功！"));
    }
    else
    {
        OnLogMsg(QString::fromLocal8Bit("连接失败！"));
    }

}

void FormTcpFileClient::on_pushButton_disconnect_clicked()
{
    _tcpClient.Disconnect();
}

void FormTcpFileClient::on_pushButton_select_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择文件"), "./");
    if(filePath=="") return;
    ui->lineEdit_filepath->setText(filePath);
    QFile file(filePath);
    QString msg;
    if(file.size()>1024*1024) {
        msg = QString::fromLocal8Bit("大小：%1M").arg(file.size()/1024/1024.0);
    }
    else
    {
        msg = QString::fromLocal8Bit("大小：%1KB").arg(file.size()/1024.0);
    }

    ui->fileSizeLabel->setText(msg);
    _tcpClient.SetFilePath(filePath);
}

void FormTcpFileClient::OnLogMsg(const QString &logmsg)
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString strlog = currentDateTime.toString("yyyy/MM/dd hh:mm:ss ")+logmsg;
    ui->plainTextEdit->appendPlainText(strlog);
}

void FormTcpFileClient::on_pushButton_send_clicked()
{
    QString strfile= ui->lineEdit_filepath->text();
    _tcpClient. transferFileInfo();
}



void FormTcpFileClient::sendCommand(MsgType type,const QString& strinfo)
{
    _tcpClient.transferCommand(type,strinfo);
}

void FormTcpFileClient::SendInit()
{
    sendCommand(MsgType::INIT_MVB);
}


void FormTcpFileClient::HandleTimer()
{
    //tcp连接显示
    if(_tcpClient.state()==QAbstractSocket::ConnectedState)
    {
         ui->btn_connect->setEnabled(false);
        ui->btn_disconnect->setEnabled(true);
         ui->btn_send->setEnabled(true);
        ui->btn_init->setEnabled(true);
    }
    else
    {
        ui->btn_connect->setEnabled(true);
        ui->btn_disconnect->setEnabled(false);
        ui->btn_send->setEnabled(false);
        ui->btn_init->setEnabled(false);
    }

    //文件发送逻辑
    QString strFilepath = ui->lineEdit_filepath->text();
    if(strFilepath.isEmpty()||_tcpClient.state()!=QAbstractSocket::ConnectedState)
    {
        ui->btn_send->setEnabled(false);
    }
    else
    {
        ui->btn_send->setEnabled(true);
    }
}

void FormTcpFileClient::connectAllSlots()
{
    connect(ui->btn_connect,&QPushButton::clicked,this,&FormTcpFileClient::on_pushButton_connect_clicked);
    connect(ui->btn_disconnect,&QPushButton::clicked,this,&FormTcpFileClient::on_pushButton_disconnect_clicked);
    connect(ui->btn_select,&QPushButton::clicked,this,&FormTcpFileClient::on_pushButton_select_clicked);
    connect(ui->btn_send,&QPushButton::clicked,this,&FormTcpFileClient::on_pushButton_send_clicked);
    connect(ui->btn_init,&QPushButton::clicked,this,&FormTcpFileClient::SendInit);

}

void FormTcpFileClient::SetTCPServerInfo(const QString &strIP, const QString &strPort)
{
    _strIP = strIP;
    _Port = strPort.toInt();

}

void FormTcpFileClient::closeEvent(QCloseEvent *event)
{
    if(_tcpClient.state()==QAbstractSocket::ConnectedState)
        _tcpClient.Disconnect();
}


